package org.example;

/**
 * 环形链表
 */
public class Solution25 {

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }

        // 定义快，慢指针
        ListNode slow = head;
        ListNode fast = head.next;

        // 遍历链表
        while (fast != null && fast.next != null) {
            if (slow == fast) {
                return true;
            }
            slow = slow.next;
            fast = fast.next.next;
        }

        return false;
    }

}
